var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), e = require("../../../@babel/runtime/helpers/createForOfIteratorHelper"), a = require("../../../@babel/runtime/helpers/asyncToGenerator"), n = require("../../../utils/tool"), o = getApp(), i = require("../../../http/activityApi.js");

Component({
    properties: {
        info: Object
    },
    data: {
        discountType: 0,
        couponSelected: null
    },
    lifetimes: {
        attached: function() {
            this.setData({
                isTejia: this.data.info.realAmount != this.data.info.scribingAmount
            }), this.loadComputeInfo();
        }
    },
    methods: {
        nothing: function() {},
        closeOnTap: function() {
            this.setData({
                info: null
            });
        },
        usePointOnTap: function() {
            this.data.isTejia || 2 == this.data.discountType || 0 == this.data.totals.sureIntegrationNum || this.setData({
                pointInfo: {
                    total: this.data.totals.memberIntegration,
                    enable: this.data.totals.sureIntegrationNum,
                    money: this.data.totals.sureIntegrationMoney
                }
            });
        },
        usePointOnConfirm: function(t) {
            this.setData({
                discountType: 1 == t.detail.selectedType ? 1 : 0
            }), this.loadComputeInfo();
        },
        couponOnSelected: function(t) {
            var e = t.detail.coupon;
            this.setData({
                couponList: null,
                discountType: e ? 2 : 0,
                couponSelected: e
            }), this.loadComputeInfo();
        },
        useCouponOnTap: function() {
            var r = this;
            return a(t.default.mark(function a() {
                var s, c, u, d;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        if (!r.data.isTejia && 1 != r.data.discountType && 0 != r.data.totals.couponCount) {
                            t.next = 2;
                            break;
                        }
                        return t.abrupt("return");

                      case 2:
                        if (r.couponList) {
                            t.next = 22;
                            break;
                        }
                        return t.prev = 3, o.showLoading(), t.next = 7, i.drawNumberActivityCoupons({
                            buyNum: 1,
                            detailsId: r.data.info.detailId
                        });

                      case 7:
                        s = t.sent, r.couponList = s.data, c = e(r.couponList);
                        try {
                            for (c.s(); !(u = c.n()).done; ) (d = u.value).isUsable = (0, n.isLessThanNow)(d.collectStartTime), 
                            d.isExpire = (0, n.getDifferentDays)(d.collectEndTime) < 7;
                        } catch (t) {
                            c.e(t);
                        } finally {
                            c.f();
                        }
                        r.setData({
                            couponList: r.couponList
                        }), t.next = 17;
                        break;

                      case 14:
                        t.prev = 14, t.t0 = t.catch(3), console.log(t.t0);

                      case 17:
                        return t.prev = 17, wx.hideLoading(), t.finish(17);

                      case 20:
                        t.next = 23;
                        break;

                      case 22:
                        r.setData({
                            couponList: r.couponList
                        });

                      case 23:
                      case "end":
                        return t.stop();
                    }
                }, a, null, [ [ 3, 14, 17, 20 ] ]);
            }))();
        },
        loadComputeInfo: function() {
            var e = this;
            return a(t.default.mark(function a() {
                var n;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.prev = 0, t.next = 3, i.drawNumberActivityCompute({
                            activityId: e.data.info.activityId,
                            detailsId: e.data.info.detailId,
                            discountType: e.data.discountType,
                            couponHistoryId: 2 == e.data.discountType ? e.data.couponSelected.couponHistoryId : null,
                            quantity: 1,
                            orderType: 0
                        });

                      case 3:
                        n = t.sent, e.setData({
                            totals: n.data
                        }), t.next = 10;
                        break;

                      case 7:
                        t.prev = 7, t.t0 = t.catch(0), console.log(t.t0);

                      case 10:
                      case "end":
                        return t.stop();
                    }
                }, a, null, [ [ 0, 7 ] ]);
            }))();
        },
        payOnTap: function() {
            var e = this;
            return a(t.default.mark(function a() {
                var n, r;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.prev = 0, o.showLoading(), t.next = 4, i.drawNumberActivityOrderCreate({
                            activityId: e.data.info.activityId,
                            detailId: e.data.info.detailId,
                            discountType: e.data.discountType,
                            couponHistoryId: 2 == e.data.discountType ? e.data.couponSelected.couponHistoryId : null,
                            quantity: 1,
                            orderType: 0
                        });

                      case 4:
                        if (n = t.sent, e.orderId = n.data.orderId, 0 != e.data.totals.discountPrice) {
                            t.next = 9;
                            break;
                        }
                        return e.paySuccess(), t.abrupt("return");

                      case 9:
                        (r = n.data.payInfo).success = function(t) {
                            console.log("【支付成功】", t), e.paySuccess();
                        }, r.fail = function(t) {
                            console.log("【支付失败】", t), e.cancelOrder();
                        }, wx.requestPayment(r), t.next = 18;
                        break;

                      case 15:
                        t.prev = 15, t.t0 = t.catch(0), console.log(t.t0);

                      case 18:
                        return t.prev = 18, wx.hideLoading(), t.finish(18);

                      case 21:
                      case "end":
                        return t.stop();
                    }
                }, a, null, [ [ 0, 15, 18, 21 ] ]);
            }))();
        },
        paySuccess: function() {
            this.closeOnTap(), this.triggerEvent("onPaySuccess", {});
        },
        cancelOrder: function() {
            o.showToast("取消支付"), i.drawNumberActivityOrderCancel(this.orderId).then(function(t) {
                console.log("【取消订单】", t);
            }).catch(function(t) {});
        }
    }
});